﻿/*
Copyright (c) 2007-2011 William POTTIER - AllProgrammic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;

namespace NetAccess.Formatters
{
    /// <summary>
    /// Partial message struc use to store received packet before treatment
    /// </summary>
    /// <remarks>for internal use only</remarks>
    internal struct PartialMessage
    {
        /// <summary>
        /// Message identifier
        /// </summary>
        internal Int32 Id;
        /// <summary>
        /// Number of Number of expected packets
        /// </summary>
        internal Int16 NumberPackets;
        /// <summary>
        /// received packets
        /// </summary>
        internal Dictionary<Int16, byte[]> Packets;
        /// <summary>
        /// Current is encrypted ?
        /// </summary>
        internal Boolean IsEncrypted;
        /// <summary>
        /// Date when reception start
        /// </summary>
        internal DateTime DateBeginReception;

        /// <summary>
        /// Initializes a new instance of the <see cref="PartialMessage"/> struct.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="numberPackets">The number packets.</param>
        /// <param name="encrypted">if set to <c>true</c> message is [encrypted].</param>
        /// <remarks></remarks>
        internal PartialMessage(Int32 id, Int16 numberPackets, Boolean encrypted)
        {
            Id = id;
            NumberPackets = numberPackets;
            IsEncrypted = encrypted;
            Packets = new Dictionary<short, byte[]>();
            DateBeginReception = DateTime.Now;
        }

        /// <summary>
        /// Determines whether this message is complete.
        /// </summary>
        /// <returns><c>true</c> if this message is complete; otherwise, <c>false</c>.</returns>
        /// <remarks></remarks>
        internal Boolean IsComplete()
        {
            return (NumberPackets == Packets.Count);
        }

        /// <summary>
        /// Gets the buffer.
        /// </summary>
        /// <returns>the buffer memoryStream</returns>
        /// <remarks></remarks>
        internal MemoryStream GetBuffer()
        {
            var ms = new MemoryStream();
            var bw = new BinaryWriter(ms);

            for (Int16 i = 1; i <= NumberPackets; i++ )
            {
                bw.Write(Packets[i]);
            }

            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
    }
}
